package com.huiyun.amnews.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.huiyun.amnews.R;
import com.huiyun.amnews.been.AppInfo;
import com.huiyun.amnews.event.DownLoadFinishEvent;
import com.huiyun.amnews.fusion.Constant;
import com.huiyun.amnews.util.ApkUtils;
import com.huiyun.amnews.util.JsonUtil;
import com.huiyun.amnews.wight.NumberProgressBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okserver.download.DownloadInfo;
import com.lzy.okserver.download.DownloadManager;
import com.lzy.okserver.download.DownloadService;
import com.lzy.okserver.download.db.DownloadDBManager;
import com.lzy.okserver.listener.DownloadListener;
import com.lzy.okserver.task.ExecutorWithListener;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class DownloadManagerActivity extends BaseActivity implements View.OnClickListener, ExecutorWithListener.OnAllTaskEndListener {

	private List<DownloadInfo> allTask;
	private MyAdapter adapter;
	private DownloadManager downloadManager;

	@Bind(R.id.listView) ListView listView;
	@Bind(R.id.t_title) TextView titleTv;
	@Bind(R.id.back_left_liner) LinearLayout back_liner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_download_manager);
		ButterKnife.bind(this);
		initView();

		downloadManager = DownloadService.getDownloadManager();
		allTask = downloadManager.getAllTask();
		adapter = new MyAdapter();
		listView.setAdapter(adapter);

		downloadManager.getThreadPool().getExecutor().addOnAllTaskEndListener(this);
	}

	private void initView(){
		titleTv.setText("下载管理");
		back_liner = findView(R.id.back_left_liner);
		back_liner.setVisibility(View.VISIBLE);
		back_liner.setOnClickListener(this);
	}

	@Override
	public void onAllTaskEnd() {
		for (DownloadInfo downloadInfo : allTask) {
			if (downloadInfo.getState() != DownloadManager.FINISH) {
//				Toast.makeText(DownloadManagerActivity.this, "所有下载线程结束，部分下载未完成", Toast.LENGTH_SHORT).show();
				return;
			}
		}
//		Toast.makeText(DownloadManagerActivity.this, "所有下载任务完成", Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		//记得移除，否者会回调多次
		downloadManager.getThreadPool().getExecutor().removeOnAllTaskEndListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		adapter.notifyDataSetChanged();
	}

	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.removeAll:
				downloadManager.removeAllTask();
				adapter.notifyDataSetChanged();  //移除的时候需要调用
				break;
			case R.id.pauseAll:
				downloadManager.pauseAllTask();
				break;
			case R.id.stopAll:
				downloadManager.stopAllTask();
				break;
			case R.id.startAll:
				downloadManager.startAllTask();
				break;
			case R.id.back_left_liner:
				finish();
				break;
		}
	}

	private class MyAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return allTask.size();
		}

		@Override
		public DownloadInfo getItem(int position) {
			return allTask.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			DownloadInfo downloadInfo = getItem(position);
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(DownloadManagerActivity.this, R.layout.item_download_manager, null);
				holder = new ViewHolder(convertView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.refresh(downloadInfo);

			//对于非进度更新的ui放在这里，对于实时更新的进度ui，放在holder中
			AppInfo apk = (AppInfo) downloadInfo.getData();
			if (apk != null) {
				Glide.with(DownloadManagerActivity.this).load(apk.getThumbnailName()).error(R.mipmap.ic_launcher).into(holder.icon);
				holder.name.setText(apk.getName());
			} else {
				holder.name.setText(downloadInfo.getFileName());
			}
			holder.download.setOnClickListener(holder);
			holder.remove.setOnClickListener(holder);

			DownloadListener downloadListener = new MyDownloadListener();
			downloadListener.setUserTag(holder);
			downloadInfo.setListener(downloadListener);
			return convertView;
		}
	}

	private class ViewHolder implements View.OnClickListener {
		private DownloadInfo downloadInfo;
		private ImageView icon;
		private TextView name;
		private TextView downloadSize;
		private TextView tvProgress;
		private TextView netSpeed;
		private NumberProgressBar pbProgress;
		private TextView download;
		private ImageView remove;

		public ViewHolder(View convertView) {
			icon = (ImageView) convertView.findViewById(R.id.icon);
			name = (TextView) convertView.findViewById(R.id.name);
			downloadSize = (TextView) convertView.findViewById(R.id.downloadSize);
			tvProgress = (TextView) convertView.findViewById(R.id.tvProgress);
			netSpeed = (TextView) convertView.findViewById(R.id.netSpeed);
			pbProgress = (NumberProgressBar) convertView.findViewById(R.id.pbProgress);
			download = (TextView) convertView.findViewById(R.id.start);
			remove = (ImageView) convertView.findViewById(R.id.remove);
		}

		public void refresh(DownloadInfo downloadInfo) {
			this.downloadInfo = downloadInfo;
			refresh();
		}

		//对于实时更新的进度ui，放在这里，例如进度的显示，而图片加载等，不要放在这，会不停的重复回调
		//也会导致内存泄漏
		private void refresh() {
			String downloadLength = Formatter.formatFileSize(DownloadManagerActivity.this, downloadInfo.getDownloadLength());
			String totalLength = Formatter.formatFileSize(DownloadManagerActivity.this, downloadInfo.getTotalLength());
			downloadSize.setText(downloadLength + "/" + totalLength);
			if (downloadInfo.getState() == DownloadManager.NONE) {
				netSpeed.setText("停止");
				download.setText("下载");
			} else if (downloadInfo.getState() == DownloadManager.PAUSE) {
				netSpeed.setText("暂停中");
				download.setText("继续");
			} else if (downloadInfo.getState() == DownloadManager.ERROR) {
				netSpeed.setText("下载出错");
				download.setText("出错");
			} else if (downloadInfo.getState() == DownloadManager.WAITING) {
				netSpeed.setText("等待中");
				download.setText("等待");
			} else if (downloadInfo.getState() == DownloadManager.FINISH) {
//				if (ApkUtils.isAvailable(DownloadManagerActivity.this, new File(downloadInfo.getTargetPath()))) {
				if (ApkUtils.isAvailable(DownloadManagerActivity.this, ((AppInfo)downloadInfo.getData()).getPackage_name())) {
					download.setText("卸载");
				} else {
					download.setText("安装");
				}
				netSpeed.setText("下载完成");
			} else if (downloadInfo.getState() == DownloadManager.DOWNLOADING) {
				String networkSpeed = Formatter.formatFileSize(DownloadManagerActivity.this, downloadInfo.getNetworkSpeed());
				netSpeed.setText(networkSpeed + "/s");
				download.setText("暂停");
			}
			tvProgress.setText((Math.round(downloadInfo.getProgress() * 10000) * 1.0f / 100) + "%");
//			pbProgress.setMax((int) downloadInfo.getTotalLength());
//			pbProgress.setProgress((int) downloadInfo.getDownloadLength());

			pbProgress.setMax(10000);
			pbProgress.setProgress((int) (Math.round(downloadInfo.getProgress() * 10000) * 1.0f / 1));
		}

		@Override
		public void onClick(View v) {
			if (v.getId() == download.getId()) {
				switch (downloadInfo.getState()) {
					case DownloadManager.PAUSE:
					case DownloadManager.NONE:
					case DownloadManager.ERROR:
						downloadManager.addTask(downloadInfo.getUrl(), downloadInfo.getRequest(), downloadInfo.getListener());
						break;
					case DownloadManager.DOWNLOADING:
						downloadManager.pauseTask(downloadInfo.getUrl());
						break;
					case DownloadManager.FINISH:
//						if (ApkUtils.isAvailable(DownloadManagerActivity.this, new File(downloadInfo.getTargetPath()))) {
						if (ApkUtils.isAvailable(DownloadManagerActivity.this, ((AppInfo)downloadInfo.getData()).getPackage_name())) {
//							ApkUtils.uninstall(DownloadManagerActivity.this, ApkUtils.getPackageName(DownloadManagerActivity.this, downloadInfo.getTargetPath()));
							ApkUtils.uninstall(DownloadManagerActivity.this, ((AppInfo)downloadInfo.getData()).getPackage_name());
						} else {
							ApkUtils.install(DownloadManagerActivity.this, new File(downloadInfo.getTargetPath()));
						}
						break;
					default:
						EventBus.getDefault().post(new DownLoadFinishEvent());
						break;
				}
				refresh();
			} else if (v.getId() == remove.getId()) {
				downloadManager.removeTask(downloadInfo.getUrl());
				adapter.notifyDataSetChanged();
				EventBus.getDefault().post(new DownLoadFinishEvent());
			}
		}
	}

	private class MyDownloadListener extends DownloadListener {

		@Override
		public void onProgress(DownloadInfo downloadInfo) {
			if (getUserTag() == null) return;
			ViewHolder holder = (ViewHolder) getUserTag();
			holder.refresh();  //这里不能使用传递进来的 DownloadInfo，否者会出现条目错乱的问题
		}

		@Override
		public void onFinish(DownloadInfo downloadInfo) {
			String packageName = ApkUtils.getPackageName(DownloadManagerActivity.this, downloadInfo.getTargetPath());
			AppInfo appInfo = (AppInfo) downloadInfo.getData();
			appInfo.setPackage_name(packageName);
			downloadInfo.setData(appInfo);
			DownloadDBManager.INSTANCE.replace(downloadInfo);
			Toast.makeText(DownloadManagerActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
			EventBus.getDefault().post(new DownLoadFinishEvent());

			setDownLoadCount(appInfo);
			ApkUtils.install(DownloadManagerActivity.this, new File(downloadInfo.getTargetPath()));
		}

		@Override
		public void onError(DownloadInfo downloadInfo, String errorMsg, Exception e) {
			if (errorMsg != null) Toast.makeText(DownloadManagerActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
		}
	}

	//下载
	public void setDownLoadCount(AppInfo appInfo) {
		HashMap<String, Object> params = new HashMap<>();
		params.put("app_id",appInfo.getId());
		String jsonData = JsonUtil.objectToJson(params);
		OkGo.post(Constant.DOWN_LOAD_COUNT_URL)
				.tag(this)
				.upJson(jsonData)
				.execute(new StringCallback() {
					@Override
					public void onSuccess(String s, Call call, Response response) {
						if (TextUtils.isEmpty(s)) return;
						Map<String, Object> dataMap = (Map<String, Object>) JsonUtil.jsonToMap(s);
						if (dataMap == null) {
							return;
						}
					}

					@Override
					public void onError(Call call, Response response, Exception e) {
					}
				});
	}
}
